package com.zhou.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhou.dao.StudentDAO;
import com.zhou.entity.Result;
import com.zhou.entity.Student;
import com.zhou.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("result")
public class ResultController {

    @Autowired
    private ResultService resultService;

    @Autowired
    private StudentDAO studentDAO;

    @CrossOrigin
    @RequestMapping("addResult")
    public Map<String, Object> addResult(
            String token,
            @RequestParam("file") MultipartFile file,
            String message_id,
            String account,
            String result_url,
            String result_name) {

        long studentid = Long.valueOf(account).longValue();
        int MessageID = Integer.valueOf(message_id).intValue();

        String studentName = studentDAO.findname(studentid);
        String filename = account +"_"+studentName+"_"+result_name;
        Result result = resultService.findResult(studentid, MessageID);
        if(result == null){
            Result result1 = new Result();
            result1.setFinish("已完成");
            result1.setCheck("未审核");
            result1.setStudent_id(studentid);
            result1.setMessage_id(MessageID);
            resultService.addResult(result1);
        }
        Map<String, Object> map = new HashMap<>();
        FileOutputStream out = null;
        try {
            // file.getOriginalFilename()包含了后缀
            String fileName = file.getOriginalFilename();
            // 获取文件后缀名
            String Suffix = fileName.substring(fileName.lastIndexOf("."));
            // 文件名为UUID+后缀（这里需要修改为其他获取名字算法然后进行拼接）
            fileName = filename + Suffix;


            // 为通过文件名为自己添加路径
            if (fileName.indexOf("\\") != -1) {
                fileName = fileName.substring(fileName.lastIndexOf("\\"));
            }
            // 获取文件存放的文件夹地址
            String filePath = result_url+"/";
            File f = new File(filePath);
            if (!f.exists()) {
                f.mkdirs();// 不存在路径则进行文件夹的创建
            }

            // 重新自定义文件的名称
            filePath = filePath + fileName;
            out = new FileOutputStream(filePath);
            out.write(file.getBytes());
            out.flush();
            out.close();


            map.put("error", "");
            map.put("state", 1);
//            Thread.sleep(10000);
            return map;
        } catch (Exception e) {
            map.clear();
            map.put("2", "error");
            return map;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
